package com.example.vacateapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.vacateapplication.eventbus.PickerMessageEvent;
import com.example.vacateapplication.picker.PickerUntil;
import com.example.vacateapplication.timeSelector.CustomDatePicker;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class VacateActivity extends AppCompatActivity implements View.OnClickListener{

    LinearLayout startTime;//开始时间
    LinearLayout endTime;//结束时间
    TextView startTimeTextView;
    TextView endTimeTextView;
    private String time;
    private String date;
    private CustomDatePicker startTimePicker,endTimePicker;
    private PickerUntil pickerUntil;
    LinearLayout leaveTypeLayout;
    TextView tvLeaverType;
    LinearLayout iKnowLayout;
    TextView tvIKnow;
    LinearLayout needKnowLayout;
    TextView tvNeedKnow;
    Button btLeaveSubmit;
    EditText etLeaveReason;
    ImageView back;
    ImageView ivLeaveRecord;
    private String strLeaveType;
    private String strStartTime;
    private String strEndTime;
    private String strLeaveTime;
    private String strLeaveReason;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vacate);

        startTime = findViewById(R.id.startTime);
        startTime.setOnClickListener(this);
        endTime = findViewById(R.id.endTime);
        endTime.setOnClickListener(this);
        startTimeTextView = findViewById(R.id.startTimeTextView);
        endTimeTextView = findViewById(R.id.endTimeTextView);
        pickerUntil = new PickerUntil(this);
        leaveTypeLayout = findViewById(R.id.leaveTypeLayout);
        leaveTypeLayout.setOnClickListener(this);
        tvLeaverType = findViewById(R.id.tv_LeaverType);
        iKnowLayout = findViewById(R.id.iKnowLayout);
        iKnowLayout.setOnClickListener(this);
        tvIKnow = findViewById(R.id.tv_iKnow);
        needKnowLayout = findViewById(R.id.needKnowLayout);
        needKnowLayout.setOnClickListener(this);
        tvNeedKnow = findViewById(R.id.tv_needKnow);
        btLeaveSubmit = findViewById(R.id.bt_leaveSubmit);
        btLeaveSubmit.setOnClickListener(this);
        etLeaveReason = findViewById(R.id.et_leaveReason);
        back = findViewById(R.id.back);
        back.setOnClickListener(this);
        ivLeaveRecord = findViewById(R.id.iv_leaveRecord);
        ivLeaveRecord.setOnClickListener(this);
        initPicker();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.startTime:
                startTimePicker.show(time);
                break;
            case R.id.endTime:
                endTimePicker.show(date);
                break;
            case R.id.leaveTypeLayout:
                pickerUntil.showLeaveTypePicker();
                break;
            case R.id.iKnowLayout:
                pickerUntil.showIKnowPicker();
                break;
            case R.id.needKnowLayout:
                pickerUntil.showNeedKnowPricker();
                break;
            case R.id.iv_leaveRecord://请假记录
                break;
            case R.id.bt_leaveSubmit:  // 提交
                String strtvIKnow = tvIKnow.getText().toString();
                String strtvNeedKnow = tvNeedKnow.getText().toString();
                strLeaveReason = etLeaveReason.getText().toString();//输入框内容
                if(strLeaveType == null){
                    Toast.makeText(VacateActivity.this, "请假类型不为空！！", Toast.LENGTH_SHORT).show();
                }else if (strStartTime == null){
                    Toast.makeText(VacateActivity.this, "开始时间不为空！！", Toast.LENGTH_SHORT).show();
                }else if (strEndTime == null){
                    Toast.makeText(VacateActivity.this, "结束时间不为空！！", Toast.LENGTH_SHORT).show();
                }else if (strtvIKnow == null || strtvIKnow.equals("")){
                    Toast.makeText(VacateActivity.this, "我已知不为空！！", Toast.LENGTH_SHORT).show();
                }else if (strLeaveReason == null || strLeaveReason.equals("")){
                    Toast.makeText(VacateActivity.this, "请假事由不为空！！", Toast.LENGTH_SHORT).show();
                }else if (strtvNeedKnow == null || strtvNeedKnow.equals("")){
                    Toast.makeText(VacateActivity.this, "需知不为空！！", Toast.LENGTH_SHORT).show();
                }else {
                    System.out.println("可以提交请假信息 -------------");
//                   System.out.println("----开始时间---"+strStartTime);
//                   System.out.println("*****结束时间*****"+strEndTime);
//                   System.out.println("*****请假类型*****"+strLeaveType);


                }
                break;
            case R.id.back:
                finish();
                break;
        }
    }

    //    时间 选择
    private void initPicker() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        time = sdf.format(new Date());
        date = sdf.format(new Date());
        //设置当前显示的时间 默认
//        startTimeTextView.setText("");
//        endTimeTextView.setText("");
        /**
         * 设置年月日
         */
//        开始时间
        startTimePicker = new CustomDatePicker(this, "请选择开始时间", new CustomDatePicker.ResultHandler() {
            @Override
            public void handle(String time) {
                startTimeTextView.setText(time);
                strStartTime = time;
            }
        }, "2022-01-01 00:00", "2035-12-31 23:59");//
        startTimePicker.showSpecificTime(true);
        startTimePicker.setIsLoop(true);

        // 结束时间
        endTimePicker = new CustomDatePicker(this, "请选择结束时间", new CustomDatePicker.ResultHandler() {
            @Override
            public void handle(String data) {
                endTimeTextView.setText(data);
                strEndTime = data;
            }
        }, "2022-01-01 00:00", "2035-12-31 23:59");//"2027-12-31 23:59"
        endTimePicker.showSpecificTime(true);
        endTimePicker.setIsLoop(true);
    }

    @Override
    public void onStart() {
        super.onStart();
        if(!EventBus.getDefault().isRegistered(this)){
            //注册事件
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (EventBus.getDefault().isRegistered(this)){
            //注销事件
            EventBus.getDefault().unregister(this);
        }
    }

    //处理选择器发送结果事件
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMoonEvent(PickerMessageEvent messageEvent){
        switch (messageEvent.getPickerType()){ // 1 请假类型 2 我已知内容 3 需知 内容
            case 1:
                tvLeaverType.setText(messageEvent.getMessage());
                strLeaveType = messageEvent.getMessage();
                break;
            case 2:
                tvIKnow.setText(messageEvent.getMessage());
                break;
            case 3:
                tvNeedKnow.setText(messageEvent.getMessage());
                break;
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
    //关闭软键盘
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideInput(v, ev)) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
            return super.dispatchTouchEvent(ev);
        }
        // 必不可少，否则所有的组件都不会有TouchEvent了
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }
    public  boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] leftTop = { 0, 0 };
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击的是输入框区域，保留点击EditText的事件
                return false;
            } else {
                return true;
            }
        }
        return false;
    }
}